package com.xialinrui.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xialinrui.entity.ConflictRecord;
import com.xialinrui.entity.ResponseResult;
import com.xialinrui.service.ConflictRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xialinrui
 */
@RestController
@Slf4j
@RequestMapping("/conflict")
public class ConflictRecordController {

    @Resource
    private ConflictRecordService conflictRecordService;

    @PostMapping("/add")
    public ResponseResult<Map<String, Integer>> addConflict(@RequestBody ConflictRecord conflictRecord) {
        conflictRecordService.addOneInstant(conflictRecord);
        HashMap<String, Long> resMap = new HashMap<>();
        resMap.put("id", conflictRecord.getId());
        return new ResponseResult(200, "创建成功", resMap);
    }

    @PutMapping("/update")
    public ResponseResult update(@RequestBody ConflictRecord conflictRecord) {
        conflictRecordService.updateById(conflictRecord);
        return new ResponseResult(200, "保存成功");
    }

    @DeleteMapping("/delete")
    public ResponseResult delete(@RequestBody List<Long> ids) {
        conflictRecordService.delete(ids);
        return new ResponseResult(200, "删除成功");
    }

    /**
     * 查询所有波澜不惊
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public ResponseResult<Page<ConflictRecord>> getAll(@RequestParam Integer page, @RequestParam Integer pageSize) {
        Page<ConflictRecord> resPage = new Page<>(page, pageSize);
        QueryWrapper<ConflictRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("update_time").orderByDesc("create_time");
        conflictRecordService.page(resPage, queryWrapper);
        return new ResponseResult(200, "查询成功", resPage);
    }

    /**
     * 根据输入框模糊查询意外记录
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/list/like")
    public ResponseResult<Page<ConflictRecord>> getLike(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam String content) {
        Page<ConflictRecord> resPage = new Page<>(page, pageSize);
        QueryWrapper<ConflictRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", content).orderByDesc("update_time").orderByDesc("create_time");
        conflictRecordService.page(resPage, queryWrapper);
        return new ResponseResult(200, "查询成功", resPage);
    }

    /**
     * 根据id显示文章详情
     *
     * @param id
     * @return
     */
    @GetMapping("/show")
    public ResponseResult<ConflictRecord> show(@RequestParam Long id) {
        return new ResponseResult(200, "显示成功", conflictRecordService.getById(id));
    }

    /**
     * 根据时间删选文章
     *
     * @param from
     * @param to
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/list/date")
    public ResponseResult<Page<ConflictRecord>> getByDate(@RequestParam Date from, @RequestParam Date to, @RequestParam Integer page, @RequestParam Integer pageSize) {
        Page<ConflictRecord> resPage = new Page<>(page, pageSize);
        QueryWrapper<ConflictRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("create_time", from, to).orderByDesc("update_time").orderByDesc("create_time");
        conflictRecordService.page(resPage, queryWrapper);
        return new ResponseResult(200, "查询成功", resPage);
    }

}
